package com.sc.test;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.sc.test.bean.URLS;
import com.sc.test.utils.GlideApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropSquareTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by Shen Chao on 2018-01-03-003.
 */

public class GlideActivity extends AppCompatActivity {

    @BindView(R.id.lv_pic1)
    ImageView lvPic1;
    @BindView(R.id.lv_pic2)
    ImageView lvPic2;
    @BindView(R.id.lv_pic3)
    ImageView lvPic3;
    @BindView(R.id.cv_pic4)
    CustomView cvPic4;
    @BindView(R.id.lv_pic5)
    ImageView lvPic5;
    @BindView(R.id.lv_pic6)
    ImageView lvPic6;
    @BindView(R.id.lv_pic7)
    ImageView lvPic7;
    @BindView(R.id.lv_pic8)
    ImageView lvPic8;
    @BindView(R.id.lv_pic9)
    ImageView lvPic9;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_glide_getpic1, R.id.bt_glide_getpic2, R.id.bt_glide_getpic3
            , R.id.bt_glide_getpic4, R.id.bt_glide_getpic5, R.id.bt_glide_getpic6,
            R.id.bt_glide_getpic7, R.id.bt_glide_getpic8, R.id.bt_glide_getpic9, R.id.bt_glide_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_glide_getpic1:
//              Glide.with(this)
//                        .load(URLS.IMAGE_URL)
//                        .into(lvGlide);
                Glide.with(this)
                        .load(URLS.IMAGE_URL)
                        //缩放
                        .thumbnail(0.1f)
//                        .apply(getRequestOptions())
                        .apply(RequestOptions.fitCenterTransform())
                        //淡入淡出
                        .transition(DrawableTransitionOptions.withCrossFade())
                        .into(lvPic1);

                break;
            case R.id.bt_glide_getpic2:
                RequestBuilder requestBuilder = Glide.with(this).asBitmap();
                requestBuilder.load(R.drawable.dong);
                requestBuilder.apply(getRequestOptions());
                requestBuilder.into(lvPic2);
                break;
            case R.id.bt_glide_getpic3:
                //不要使用匿名内部类，有可能图片没加载被系统回收
                SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>(300, 300) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        lvPic3.setImageBitmap(resource);
                    }
                };
                //创建一个类继承AppGlideModule 加上@GlideModule 否则没有glideApp
                GlideApp.with(this)
                        .asBitmap()
                        .load(URLS.IMAGE_URL)
                        .dontAnimate()
                        .skipMemoryCache(true)
                        .override(230, 230)
                        .fitCenter()
                        .into(simpleTarget);
                break;
            case R.id.bt_glide_getpic4:
                ViewTarget viewTarget = new ViewTarget<CustomView, Drawable>(cvPic4) {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        this.view.setImage(resource);
                    }
                };
                Glide.with(this)
                        .load(URLS.IMAGE_URL)
                        .into(viewTarget);

                break;
            case R.id.bt_glide_getpic5:
                GlideApp.with(this)
                        .load(R.drawable.dong)
                        .skipMemoryCache(true)
                        .fitCenter()
                        .thumbnail(Glide.with(this)
                                .load(R.drawable.show))
                        .transition(DrawableTransitionOptions.withCrossFade(1500))
//                        .transition(new DrawableTransitionOptions().dontTransition())
//                        .transition(DrawableTransitionOptions.with(NoTransition.<Drawable>getFactory()))
                        .into(lvPic5);
                break;
            case R.id.bt_glide_getpic6:
                //圆形
                GlideApp.with(this)
                        .load(URLS.IMAGE_URL)
                        .circleCrop()
                        .skipMemoryCache(true)
                        .into(lvPic6);
                break;
            case R.id.bt_glide_getpic7:
                //模糊
                GlideApp.with(this)
                        .load(URLS.IMAGE_URL)
                        .transform(new BlurTransformation(50))
//                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(50)))
                        .skipMemoryCache(true)
                        .into(lvPic7);
                break;
            case R.id.bt_glide_getpic8:
                //正方形
                GlideApp.with(this)
                        .load(URLS.IMAGE_URL)
                        .transform(new CropSquareTransformation())
//                        .apply(RequestOptions.bitmapTransform(new CropSquareTransformation()))
                        .skipMemoryCache(true)
                        .into(lvPic8);
                break;
            case R.id.bt_glide_getpic9:
                //圆角
                //RoundedCornersTransformation(int radius, int margin, CornerType cornerType)
                //两个参数 radius是圆角大小 margin是图片外边界大小 cornerType为哪个角圆角
                GlideApp.with(this)
                        .load(URLS.IMAGE_URL)
                        .fitCenter()
                        .transform(new RoundedCornersTransformation(50, 0, RoundedCornersTransformation.CornerType.ALL))
                        .skipMemoryCache(true)
                        .into(lvPic9);
                break;
            case R.id.bt_glide_clear:
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Glide.get(getApplicationContext())
//                                .clearDiskCache();
//                    }
//                }).start();
//
//                Glide.get(this)
//                        .clearMemory();
                lvPic1.setImageBitmap(null);
                lvPic2.setImageBitmap(null);
                lvPic3.setImageBitmap(null);
                cvPic4.setImage(null);
                lvPic5.setImageBitmap(null);
                lvPic6.setImageBitmap(null);
                lvPic7.setImageBitmap(null);
                lvPic8.setImageBitmap(null);
                lvPic9.setImageBitmap(null);
                break;
                default:
                    break;
        }
    }

    public RequestOptions getRequestOptions() {
        return new RequestOptions()
//        CenterCrop 缩放宽和高都到达View的边界，有一个参数在边界上，另一个参数可能在边界上，也可能超过边界
//        CenterInside 如果宽和高都在View的边界内，那就不缩放，否则缩放宽和高都进入View的边界，
//                      有一个参数在边界上，另一个参数可能在边界上，也可能在边界内
//        CircleCrop 圆形且结合了CenterCrop的特性
//        FitCenter 缩放宽和高都进入View的边界，有一个参数在边界上，另一个参数可能在边界上，也可能在边界内
//        RoundedCorners 圆角
                //按比例填充imageview 可能显示不完整
//                        .centerCrop()
                //图片缩放 显示完整 可能填不满imageview
                .fitCenter()
//                DiskCacheStrategy.ALL 使用DATA和RESOURCE缓存远程数据，仅使用RESOURCE来缓存本地数据。
//                DiskCacheStrategy.NONE 不使用磁盘缓存
//                DiskCacheStrategy.DATA 在资源解码前就将原始数据写入磁盘缓存
//                DiskCacheStrategy.RESOURCE 在资源解码后将数据写入磁盘缓存，即经过缩放等转换后的图片资源。
//                DiskCacheStrategy.AUTOMATIC 根据原始图片数据和资源编码策略来自动选择磁盘缓存策略。默认
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                //不加载动画
                .dontAnimate()
                //跳过内存缓存
                .skipMemoryCache(true)
                //.override(200,200)
                .placeholder(R.drawable.lodding)
//                        加载优先级
//                       Priority.LOW
//                       Priority.NORMAL
//                       Priority.HIGH
//                       Priority.IMMEDIAT
                .priority(Priority.HIGH)
                .error(R.drawable.error)
                .fallback(R.drawable.show);
    }

}
